package com.cjh.filter;


import com.alibaba.fastjson.JSON;
import com.cjh.common.BaseContext;
import com.cjh.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session=httpServletRequest.getSession();
        if (session.getAttribute("employee") != null) {
            BaseContext.setCurrentId((Long) session.getAttribute("employee"));
            log.info("pc端用户已经登录,用户id为:{}",session.getAttribute("employee"));
            return true;
        }

        if (session.getAttribute("user") != null) {
            log.info("移动端用户已经登录,用户id为:{}",session.getAttribute("user"));
            BaseContext.setCurrentId((Long) session.getAttribute("user"));
            return true;
        }

        httpServletResponse.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return false;


    }
}
